package com.manelrodriguez.applications.eurostoxx50.activity;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.manelrodriguez.applications.eurostoxx50.R;
import com.manelrodriguez.applications.eurostoxx50.adapters.CarteraListAdapter;
import com.manelrodriguez.applications.eurostoxx50.bo.MovimientoBO;
import com.manelrodriguez.applications.eurostoxx50.to.Movimiento;
import com.manelrodriguez.applications.eurostoxx50.utils.Constantes;

public class CarteraActivity extends Activity implements OnItemClickListener{
	private Intent intent;
	private static List<Movimiento> movimientos;
	private CarteraListAdapter cartera_adapter;
	NumberFormat numberFormat;
	private MovimientoBO movBO;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cartera);
		numberFormat = NumberFormat.getInstance();
		numberFormat.setMaximumFractionDigits(2);
		movBO = new MovimientoBO(this);
		movimientos = movBO.getMovimientosAgrupados();
		if(movimientos!=null && movimientos.size()>0){
			this.cartera_adapter = new CarteraListAdapter(this, R.layout.cartera_item, movimientos);
			ListView lv2 = (ListView)findViewById(R.id.listView2);
			lv2.setAdapter(this.cartera_adapter);
	        lv2.setOnItemClickListener(this);
	        this.registerForContextMenu(lv2);
		}
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_cartera, menu);
        return true;
    }
	
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
	        case R.id.menu_anadir_movimiento:
	        	intent = new Intent(getApplicationContext(), CarteraAddIemActivity.class);
	    		startActivityForResult(intent,1);
	            return true;
	        case R.id.menu_ver_movimientos:
	        	intent = new Intent(getApplicationContext(), CarteraVerMovimientosActivity.class);
	        	intent.putExtra(Constantes.EMPRESA, "");
	    		startActivity(intent);
	        	return true;
	        default:
	            return super.onOptionsItemSelected(item);
        }
    }
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode==RESULT_OK && requestCode==1){
			//movimientos = movBO.getMovimientosAgrupados();
			Movimiento mov = (Movimiento)data.getExtras().getParcelable("MOVIMIENTO");
			if(this.cartera_adapter == null){
				movimientos = new ArrayList<Movimiento>();
				movimientos.add(mov);
				cartera_adapter = new CarteraListAdapter(this, R.layout.cartera_item, movimientos);
			}else{
				cartera_adapter.add(mov);
			}
			//this.cartera_adapter = new CarteraListAdapter(this, R.layout.cartera_item, movimientos);
			Toast.makeText(this, getString(R.string.anadido), Toast.LENGTH_SHORT).show();
			cartera_adapter.notifyDataSetChanged();
		}
	}
	
	@Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
		Movimiento mov = movimientos.get(info.position);
	    menu.setHeaderTitle(mov.getIdEmpresa());
	    menu.add(0, v.getId(), 0, getString(R.string.verDetalleCartera));
	    menu.add(0, v.getId(), 0, getString(R.string.borrar));
    }
	
	@Override
    public boolean onContextItemSelected(MenuItem item) {
    	AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    	if (item.getTitle().equals(getString(R.string.verDetalleCartera))) {
    		intent = new Intent(getApplicationContext(), CarteraVerMovimientosActivity.class);
        	intent.putExtra(Constantes.EMPRESA, movimientos.get(info.position).getIdEmpresa());
    		startActivity(intent);
    	}else if (item.getTitle().equals(getString(R.string.borrar))){
    		movBO.borrarMovimientosEmpresa(movimientos.get(info.position).getIdEmpresa());
    		cartera_adapter.remove(cartera_adapter.getItem(info.position));
    		Toast.makeText(CarteraActivity.this, getString(R.string.borrado), Toast.LENGTH_SHORT).show();
    		cartera_adapter.notifyDataSetChanged();
    	}

       return true;
    }

	public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
		intent = new Intent(getApplicationContext(), CarteraVerMovimientosActivity.class);
    	intent.putExtra(Constantes.EMPRESA, movimientos.get(position).getIdEmpresa());
		startActivity(intent);
	}
}
